﻿
#include "Notify.h"
#include "FrameWindow.h"
#include "Resource.h"


CFrameWindow::CFrameWindow()
{
	m_pMenUI = NULL;
};

CFrameWindow::~CFrameWindow()
{
};

void CFrameWindow::OnNotify(MUINOTIFY &notify)
{
	//想知道wParam, lParam对应了什么参数吗？说实话，由于项目还处于初期，
	//我也说不准wParam, lParam到底对应了什么参数，自己在断点中拦截到对应
	//的通知后，看[调用堆栈]追踪一下吧。
	CControlUI * pControl = (CControlUI*) notify.pSender;
	switch(notify.dwType)
	{
	case EnumNotify::ClickItem:
	{
        if (pControl->GetName() == L"btnOK")
	    {
			OnClick_Login();
		}
		else if (pControl->GetName() == L"menu_test")
		{
			OnClick_Test();
		}
		else if (pControl->GetName() == L"menu_logout")
		{
			OnClick_Logout();
		}
		else if (pControl->GetName() == L"menu_looked")
		{
			OnClick_Looked();
		}
		else if (pControl->GetName() == L"menu_close")
		{
			OnClick_Close();
		}
	}break;
	case EnumNotify::ActiveItem:
		{
			pControl = (CControlUI*)notify.lParam;
			//MessageBox(m_hWnd, pConntrol->GetClassName(), pConntrol->GetText(), NULL);
		}break;
	case EnumNotify::TimerCall:
		{

		}break;
	case EnumNotify::CheckItem:
		{

		}break;
	}

	//return __super::OnNotify(notify);
};

LRESULT CFrameWindow::OnEvent(MUIEVENT &event)
{
	CDragItemUI * pDragItem = NULL;
	switch (event.dwType)
	{
	case EnumEvent::WindowInit:
		{
			OnCreate(event.wParam, event.lParam);
			//SetTimer(m_hWnd, 100, 5000, NULL);
		}break;
	case EnumEvent::WindowReady:
		{
			OnReady(event.wParam, event.lParam);
		}break;
	case EnumEvent::DragOver:
		{
			
		}break;
	case EnumEvent::OnTimer:
		{
			if (event.wParam == 100) // 检查哪个计时器触发了消息
			{
				// 执行计时器事件处理代码
				ShowError(TRUE, L"执行计时器事件处理代码");
			}
		}break;
	case EnumEvent::WindowDestroy:
	{
		if (m_pMenUI)
		{
			m_pMenUI->Destroy();
			delete m_pMenUI;
			m_pMenUI = NULL;
		}

	}break;
	default:
		break;
	}

	return 0;
	//return __super::OnEvent(event);
}

LRESULT CALLBACK CFrameWindow::WndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	CControlUI * pControl = NULL;
	POINT point;
	CDragItemUI * pDragItem = NULL;
	SIZE szRound = {10,10};
	BYTE keyState[256] = { 0 };
	switch (message)
	{
	case UWM_LOGIN_NOTIFY:
	{
		EnableLogin(TRUE);
		if (0 == wParam)
		{
			//登录成功
			::ShowWindow(m_hWnd, FALSE);
		}
		else
		{
			LPCWSTR strError = L"";
			switch (LOGIN_WPARAM_CONNECT_FAILED)
			{
			case LOGIN_WPARAM_CONNECT_FAILED:
			{
				strError = L"连接Windows 服务失败";
			}
			default:
				break;
			}
			ShowError(TRUE, strError);
		}
		return TRUE;
	}break;
	case UWM_LOGOUT_NOTIFY:
	{

	}break;
	case UWM_TRAYICON:
	{
		if (WM_RBUTTONDOWN == lParam)
		{
			GetCursorPos(&point);
			RECT rcPos;
			rcPos.left = point.x;
			rcPos.top = point.y;

			rcPos.right = rcPos.left;
			rcPos.bottom = rcPos.top;
			//::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rcPos, 2);

			SIZE size = m_pMenUI->GetSize();
			point = CalcPopupPoint(m_hWnd, &rcPos, &size, CPOT_RIGHT);
			point.y -= size.cy / 2;
			m_pMenUI->Popup(this, point, FALSE);
		}
		else if (WM_LBUTTONDBLCLK == lParam)
		{

		}
	}break;
	case WM_LBUTTONDOWN:
		{
			point.x = GET_X_LPARAM(lParam);
			point.y = GET_Y_LPARAM(lParam);
		}break;
	case WM_MOUSEWHEEL:
	{
		if (FALSE == GetKeyboardState(keyState))
		{
			break;
		}

		if (keyState[VK_CONTROL] & 128)
		{
			int nRatio = this->GetDpiRatio();
			int nShift = (short)HIWORD(wParam);

			nRatio += nShift / 30;
			this->SetDpiRatio(nRatio);
			::UpdateWindow(m_hWnd);
			::InvalidateRect(m_hWnd, NULL, FALSE);
			return 0;
		}
		
	}break;
	default:
		break;
	}
	return __super::WndProc(message, wParam, lParam);
}


LRESULT CFrameWindow::OnCreate(WPARAM wParam, LPARAM lParam)
{
	TCHAR strSkin[MAX_PATH] = {0};
	wsprintf(strSkin, _T("file='%s'"), _T("skin/"));
	SetSkin(strSkin);

	CBuilder* pBuilder = new CBuilder(this, nullptr);
	CControlUI* pRootLayout = pBuilder->Create(strSkin, _T("frame.xml"));
	delete pBuilder;
	this->AttachFrameView(pRootLayout);

	m_NofityIcon.cbSize = sizeof(NOTIFYICONDATA);
	m_NofityIcon.hWnd = m_hWnd;
	m_NofityIcon.uID = 1000;
	m_NofityIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
	m_NofityIcon.uCallbackMessage = UWM_TRAYICON;
	m_NofityIcon.hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_CLIENT));
	lstrcpy(m_NofityIcon.szTip, TEXT("数据围墙客户端"));
	Shell_NotifyIcon(NIM_ADD, &m_NofityIcon);

	m_pMenUI = new CMenuUI();
	m_pMenUI->Create(m_hInstance, _T("MYMENU"), strSkin, _T("menu.xml"));

	return 0;
}

LRESULT CFrameWindow::OnReady(WPARAM wParam, LPARAM lParam)
{
	return 0;
}

BOOL CFrameWindow::ShowError(BOOL bShow, LPCTSTR strError)
{
	CControlUI* pControl = FindControl(L"txtError");
	if (!pControl)
	{
		return FALSE;
	}
	pControl->SetVisible(bShow);
	pControl->SetText(strError);
	pControl->Invalidate();
	//InvalidateRect(m_hWnd, NULL, NULL);
	return TRUE;
}

void CFrameWindow::EnableLogin(BOOL bEnable)
{
	CControlUI* pAccess = FindControl(L"edtAccess");
	CControlUI* pPassword = FindControl(L"edtPassword");
	CControlUI* pButton = FindControl(L"btnOK");

	pAccess->SetEnabled(bEnable);
	pPassword->SetEnabled(bEnable);
	pButton->SetEnabled(bEnable);
}

BOOL CFrameWindow::OnClick_Test()
{
	CPipe::GetInstan().Test();
	return TRUE;
}

BOOL CFrameWindow::OnClick_Login()
{
	CControlUI* pAccess = FindControl(L"edtAccess");
	CControlUI* pPassword = FindControl(L"edtPassword");

	if (!pAccess || !pPassword)
	{
		return FALSE;
	}

	m_strAccess = pAccess->GetText();
	m_strPassword = pPassword->GetText();

	if (m_strAccess.IsEmpty() || m_strPassword.IsEmpty())
	{
		return FALSE;
	}

	EnableLogin(FALSE);
	CPipe::GetInstan().Login(m_hWnd, m_strAccess, m_strPassword);
	InvalidateRect(m_hWnd, NULL, NULL);
}

BOOL CFrameWindow::OnClick_Logout()
{
	CPipe::GetInstan().Logout();
	return TRUE;
}

BOOL CFrameWindow::OnClick_Looked()
{
	CPipe::GetInstan().GetPolice();
	return TRUE;
}

BOOL CFrameWindow::OnClick_Close()
{
	CPipe::GetInstan().Logout();
	Close();
	return TRUE;
}